package cos.android.tarificador.databasehandler;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class DataBaseHandler extends ContentProvider {

	//private static final String TAG = "NotesDbAdapter";

	private static final String DATABASE_NAME = "tarificador";
	private static final String DATABASE_TASK_TABLE = "task";
	private static final String DATABASE_KIND_TABLE = "kind";

	private static final int DATABASE_VERSION = 3;

	private static final UriMatcher sUriMatcher;

	private static HashMap<String, String> kindProjectionMap;
	private static HashMap<String, String> taskProjectionMap;

	static {

		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(Constants.AUTHORITY, "task", Constants.TASK);
		sUriMatcher.addURI(Constants.AUTHORITY, "kind", Constants.KIND);
		
		taskProjectionMap= new HashMap<String, String>();
		taskProjectionMap.put(Constants._IDTASK, Constants._IDTASK);
		taskProjectionMap.put(Constants.TASKNAME, Constants.TASKNAME);
		taskProjectionMap.put(Constants.ENDS, Constants.ENDS);
		taskProjectionMap.put(Constants.STARTTIME, Constants.STARTTIME);
		taskProjectionMap.put(Constants.ENDTIME, Constants.ENDTIME);

	}

	private static final String KIND_CREATE = "create table kind (_idKind integer primary key autoincrement,"
			+ "color text not null, kindName text not null)";

	private static final String TASK_CREATE = "create table task (_idTask integer primary key autoincrement,"
			+ "taskName text not null, ends integer not null, "
			+ "startTime integer, endTime integer)";

	private static final class TarificadorDataBaseHelper extends
			SQLiteOpenHelper {

		public TarificadorDataBaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(KIND_CREATE);
			db.execSQL(TASK_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			
		}
		
		@Override
		public void onOpen(SQLiteDatabase db) {

			super.onOpen(db);
		
		}
	}

	private TarificadorDataBaseHelper tarificadorHelper;

	@Override
	public boolean onCreate() {
		tarificadorHelper = new TarificadorDataBaseHelper(getContext());
		return true;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = tarificadorHelper.getWritableDatabase();

		int count;
		switch (sUriMatcher.match(uri)) {
		case Constants.TASK:
			count = db.delete(DATABASE_TASK_TABLE, selection, selectionArgs);
			break;
		case Constants.KIND:
			count = db.delete(DATABASE_KIND_TABLE, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI" + uri);

		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {

		switch (sUriMatcher.match(uri)) {

		case Constants.TASK:
			return Constants.TASK_CONTENT_TYPE;

		case Constants.KIND:
			return Constants.KIND_CONTENT_TYPE;

		default:
			throw new IllegalArgumentException("Unknow URI" + uri);

		}

	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		// TODO COMPROBAR VALORES
		
		System.out.println("nas");
		
		SQLiteDatabase db = tarificadorHelper.getWritableDatabase();
		ContentValues val;
		if (values != null) {
			val = new ContentValues(values);
		} else {
			val = new ContentValues();
		}
		long rowId;
		switch (sUriMatcher.match(uri)) {
		case Constants.TASK:

			rowId = db.insert(DATABASE_TASK_TABLE, Constants.TASKNAME, val);
			if (rowId > 0) {
				Uri returnUri = ContentUris.withAppendedId(Constants.CONTENT_URI_TASK,
						rowId);
				getContext().getContentResolver().notifyChange(returnUri, null);
				return returnUri;
			}

		case Constants.KIND:
			rowId = db.insert(DATABASE_KIND_TABLE, Constants.KINDNAME, val);
			if (rowId > 0) {
				Uri returnUri = ContentUris.withAppendedId(Constants.CONTENT_URI_KIND,
						rowId);
				getContext().getContentResolver().notifyChange(returnUri, null);
				return returnUri;
			}
		}
		return null;

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch (sUriMatcher.match(uri)) {
		case Constants.TASK:
			qb.setTables(DATABASE_TASK_TABLE);
			qb.setProjectionMap(taskProjectionMap);
			break;
		case Constants.KIND:

			qb.setTables(DATABASE_KIND_TABLE);
			qb.setProjectionMap(kindProjectionMap);
			break;
		}

		SQLiteDatabase db = tarificadorHelper.getReadableDatabase();
		
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);
		
		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		SQLiteDatabase db = tarificadorHelper.getWritableDatabase();
		int count;
		switch (sUriMatcher.match(uri)) {
		case Constants.KIND:
			count = db.update(DATABASE_KIND_TABLE, values, selection,
					selectionArgs);
			break;
		case Constants.TASK:
			count = db.update(DATABASE_TASK_TABLE, values, selection,
					selectionArgs);

			break;

		default:
			throw new IllegalArgumentException("Unknow URI" + uri);

		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
